#pragma once
#include <iostream>
#include <assert.h>
#include "ReverseIterator.h"

using namespace std;

namespace ywx {
	template<class T>
	struct list_node {
		list_node<T>* _next;
		list_node<T>* _prev;
		T _val;

		list_node(const T& val = T())
			:_next(nullptr)
			,_prev(nullptr)
			,_val(val)
		{}
	};

	template<class T, class Ref>
	struct __list_iterator {
		typedef list_node<T> Node;

		__list_iterator(Node* node)
			:_node(node)
		{}

		__list_iterator<T, Ref>& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		__list_iterator<T, Ref> operator++(int)
		{
			__list_iterator<T, Ref> tmp = *this;
			_node = _node->_next;
			return tmp;
		}

		__list_iterator<T, Ref>& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		__list_iterator<T, Ref> operator--(int)
		{
			__list_iterator<T, Ref> tmp = *this;
			_node = _node->_prev;
			return tmp;
		}

		Ref operator*()
		{
			return _node->_val;
		}

		bool operator!=(const __list_iterator<T, Ref>& it) const
		{
			return _node != it._node;
		}

		bool operator==(const __list_iterator<T, Ref>& it) const
		{
			return _node == it._node;
		}
		
		Node* _node;
	};

	template<class T>
	class list {
	public:
		typedef list_node<T> Node;
		typedef __list_iterator<T, T&> iterator;
		typedef __list_iterator<T, const T&> const_iterator;
		typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;

		list()
		{
			_size = 0;
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		

		iterator begin()
		{
			return _head->_next;
		}

		const_iterator begin() const
		{
			return _head->_next;
		}

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}

		iterator end()
		{
			return _head;
		}

		const_iterator end() const
		{
			return _head;
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}

		void push_back(const T& val)
		{
			insert(end(), val);
		}

		void pop_front()
		{
			erase(begin());
		}

		void pop_back()
		{
			erase(--end());
		}

		iterator insert(iterator pos, const T& val)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(val);
			newnode->_next = cur;
			newnode->_prev = prev;

			cur->_prev = newnode;
			prev->_next = newnode;
			++_size;
			return newnode;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			
			prev->_next = next;
			next->_prev = prev;
			delete cur;
			--_size;
			return next;
		}	

	private:
		size_t _size;
		Node* _head;
	};

	void test_list1()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);
		for (auto e : lt)
		{
			cout << e << ' ';
		}
		cout << endl;
		list<int>::reverse_iterator rit = lt.rbegin();
		while(rit != lt.rend())
		{
			cout << *rit << ' ';
			++rit;
		}
		cout << endl;
	}

}